const path = require('path');
const webpack = require('webpack');
const _ = require('lodash/object');
const getEntryObj = require('../webpack_entry_config')
const ExtractTextPlugin = require("extract-text-webpack-plugin");
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
const getTemplat = require('../mutilpleConfig');
const CleanWebpackPlugin = require('clean-webpack-plugin')
const htmlTemp = getTemplat();
let publicPath = '';
const uglifyJs = new UglifyJSPlugin();
var _plugins = []; //插件数组
let obj = _.assign(getEntryObj('m'), {});//入口配置
const __path__ = require('../dirPath');

//css
const extractSass = new ExtractTextPlugin({
    filename: './[name].[contenthash].css',
    disable: false,
    allChunks: true
});

let pathsToClean = [
    'dist',
    'build'
]

// the clean options to use
let cleanOptions = {
    root: path.resolve(__path__),
    verbose: true,
    dry: false
}

_plugins = [
    uglifyJs,
    extractSass,
    new CleanWebpackPlugin(pathsToClean, cleanOptions),
    new webpack.optimize.CommonsChunkPlugin({
        name: 'vendor',
        minChunks: Infinity,
    }),

]
_plugins = _plugins.concat(htmlTemp);

module.exports = {
    entry: _.assign(obj.entry, {
        vendor: ['./mod/tools']
    }),

    output: {
        filename: '[name].[chunkhash].bundle.js',
        path: path.resolve(__path__, 'dist/'),
        publicPath: publicPath
    },

    module: {
        rules: [{
            test: /\.scss$/,
            use: extractSass.extract({
                use: [{
                    loader: "css-loader",
                    options: {
                        minimize: true
                    }
                }, {
                    loader: 'postcss-loader',
                    options: {
                        sourceMap: true,
                        config: {
                            path: 'postcss.config.js' // 这个得在项目根目录创建此文件
                        }
                    }
                }, {
                    loader: "sass-loader"
                }],
                // 在开发环境使用 style-loader
                fallback: "style-loader"
            })
        },
        {
            test: /\.(png|jpg|gif|svg|eot|woff|ttf)$/,
            loader: 'file-loader',
            options: {
                name: '[name].[ext]',
            }
        },
        {
            test: /\.js$/,
            exclude: /(node_modules|bower_components)/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: ['env', 'stage-0'],
                    plugins: ['transform-runtime']
                }
            }
        }]
    },

    plugins: _plugins
}